import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_picker/image_picker.dart';
import 'package:xiandou/Request/DioManager.dart';
import 'package:xiandou/Request/NWApi.dart';
import 'package:xiandou/Request/NWMethod.dart';
import 'package:xiandou/Views/XDLoading.dart';
import 'package:xiandou/common/Global.dart';
import 'package:xiandou/models/Index.dart';
import '../Common.dart';
import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';



class OrderSubmitPage extends StatefulWidget {
  @override
  TaskDetailsModel taskDetailModel = new TaskDetailsModel();
  OrderSubmitPage({Key key, @required this.taskDetailModel})
      : super(key:key);
  State<StatefulWidget> createState() => _OrderSubmitPageState();
//  _OrderSubmitPageState createState() => _OrderSubmitPageState();
}

class _OrderSubmitPageState extends State<OrderSubmitPage> {

  Future<void> SubmitTaskAction() async {
//    var params = {'taskId':widget.taskDetailModel.taskId.toString()};
//    DioManager().request(NWMethod.POST, NWApi.taskSubmitApi,params: params,success: (data){
//      Fluttertoast.showToast(msg: '提交成功');
//    },
//    error: (error){
//      Fluttertoast.showToast(msg: error.message);
//    });
    if(_image == null){
      Fluttertoast.showToast(msg: '请先选择图片');
      return;
    }
    String path = _image.path;
    var name = path.substring(path.lastIndexOf("/") + 1, path.length);
    FormData formData = FormData.fromMap({
      "taskId": widget.taskDetailModel.taskId.toString(),
      "homePageLink": 'https://www.baidu.com',
      "file":  await MultipartFile.fromFile(_image.path, filename: name)
    });

//    DioManager().request(NWMethod.POST, NWApi.SubmitProofApi,params: formData,success: (data){
//      print(data);
//    },error: (error){
//      Fluttertoast.showToast(msg: error.message);
//    });
    XDProgressDialog.showProgress(context);
    DioManager().request(NWMethod.POST, NWApi.taskSubmitApi,params: formData,success: (data){
      print(data);
      XDProgressDialog.dismiss(context);
      EasyLoading.showSuccess('上传成功');
    },error: (error){
      XDProgressDialog.dismiss(context);
      Fluttertoast.showToast(msg: error.message);
    });
  }

  File _image;
  final picker = ImagePicker();
  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);
    _image = File(pickedFile.path);
    setState(() {
    });
  }

  @override
  Widget build(BuildContext context) {
    final titleString = widget.taskDetailModel.taskTitle;
    // TODO: implement build
    return FlutterEasyLoading(
      child: Scaffold(
          appBar: CupertinoNavigationBar(
            backgroundColor: Colors.white,
            middle: Text(titleString),
          ),
          body: Container(
              color: Colors.white,
              child: Column(
                children: <Widget>[
                  Expanded(
                      child: ListView(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.topLeft,
                            padding: const EdgeInsets.only(left: 21,top: 30),
                            child: Text(widget.taskDetailModel.taskTitle,style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold),),
                          ),
                          Container(
                            padding: const EdgeInsets.only(left: 21,top: 16,right: 21),
                            child: Row(
                              children: <Widget>[
                                Text.rich(TextSpan(
                                    children: [
                                      TextSpan(text: '当前参与人数',style: TextStyle(color: Common.darkColor,fontSize: 15,fontWeight: FontWeight.w500)),
                                      TextSpan(text: widget.taskDetailModel.taskAttendanceNumber.toString()+'       ',style: TextStyle(color: Common.xdDeepRedColor,fontSize: 15,fontWeight: FontWeight.bold)),
                                    ]
                                )),
                                Text.rich(TextSpan(
                                    children: [
                                      TextSpan(text: '累计参与人数',style: TextStyle(color: Common.darkColor,fontSize: 15,fontWeight: FontWeight.w500)),
                                      TextSpan(text: widget.taskDetailModel.taskMaxJoinNumber.toString(),style: TextStyle(color: Common.xdDeepRedColor,fontSize: 15,fontWeight: FontWeight.bold)),
                                    ]
                                ))
                              ],
                            ),
                          ),
                          Container(
                              height: 100,
                              margin: const EdgeInsets.only(top: 10),
                              child:Row(
                                mainAxisAlignment: MainAxisAlignment.center ,
                                children: <Widget>[
                                  Expanded(
                                    child:Column(
                                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                      children: <Widget>[
                                        Text('任务红包奖励',style: TextStyle(color: Color(0xff666666),fontSize: 15,fontWeight: FontWeight.w500),),
                                        Text(widget.taskDetailModel.award.toString(),style: TextStyle(color: Color(0xffFFBD18),fontSize: 20,fontWeight: FontWeight.bold)),
                                      ],
                                    ),
                                    flex: 1,
                                  ),
                                  Expanded(
                                    child:Column(
                                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                      children: <Widget>[
                                        Text('截止时间',style: TextStyle(color: Color(0xff666666),fontSize: 15,fontWeight: FontWeight.w500)),
                                        Text(widget.taskDetailModel.endTime,style: TextStyle(color: Color(0xff333333),fontSize: 17,fontWeight: FontWeight.bold)),
                                      ],
                                    ),
                                    flex: 1,
                                  ),
                                ],
                              )
                          ),
                          Container(
                            height: 1,
                            margin: const EdgeInsets.only(left: 10,right: 10),
                            decoration: new BoxDecoration(
                              color: Color(0xffEEEEEE),
                            ),
                          ),
                          Container(
                            padding: const EdgeInsets.only(left: 20,top: 30,right: 10),
                            alignment: Alignment.topLeft,
                            child: Column(
                              crossAxisAlignment:CrossAxisAlignment.stretch,
                              children: <Widget>[
                                Text('任务时间',style: TextStyle(color: Color(0xff333333),fontSize: 17,fontWeight: FontWeight.bold)),
                                Padding(
                                  padding: const EdgeInsets.only(top: 18),
                                  child: Text.rich(TextSpan(
                                      children: [
                                        TextSpan(text: '1.打开抖音app，找到需要援助的抖音账号，观看视频不少于30S； \n '
                                            '2.任务要求：点赞，评论，此条抖音或关注该条抖音博主，当白色心变成红色，表示点赞成功；',style: TextStyle(color: Color(0xff666666),fontSize: 16,fontWeight: FontWeight.w500)),
                                        TextSpan(text: widget.taskDetailModel.taskDescription,style: TextStyle(color: Color(0xffFF5236),fontSize: 14,fontWeight: FontWeight.bold)),
                                      ]
                                  )),
                                )
                              ],
                            ),
                          ),
                          Container(
                            height: 368,
                            margin: const EdgeInsets.all(10),
                            decoration: new BoxDecoration(
                              color: Colors.white,
                              borderRadius: new BorderRadius.circular((5.0)),
                            ),
                            child: Container(
                              padding: const EdgeInsets.only(left: 10,top: 27.5),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text('上传抖音个人中心页面截图',style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold,fontSize: 17),),
                                  Expanded(
                                      child: Padding(
                                        padding: const EdgeInsets.only(top: 25),
                                        child: Row(
                                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                          children: <Widget>[
                                            Column(
                                              children: <Widget>[
                                                Text('样图'),
                                                Container(
                                                  margin: const EdgeInsets.only(top: 10,bottom: 10),
                                                  height: 217,
                                                  width: 151,
                                                  child: Image(image: AssetImage('assets/dy_example.png'),),
                                                ),
                                                Text('点击查看大图',style: TextStyle(color: Common.darkColor),)
                                              ],
                                            ),
                                            Column(
                                              children: <Widget>[
                                                Text('上传抖音个人中心截屏'),
                                                GestureDetector(
                                                  onTap: getImage,
                                                  child: Container(
                                                      height: 217,
                                                      width: 151,
                                                      margin: const EdgeInsets.only(top: 10,bottom: 10),
                                                      child: _image == null
                                                          ? Image(image: AssetImage('assets/d_upload.png'),)
                                                          : Image.file(_image)
                                                  ),
                                                )
                                              ],
                                            ),
                                          ],
                                        ),
                                      )
                                  )
                                ],
                              ),
                            ),
                          ),
                        ],
                      )
                  ),
                  Container(
                    padding: const EdgeInsets.only(left: 0,right: 0,bottom: 0),
                    height: 54.5,
                    width: MediaQuery.of(context).size.width,
                    decoration: new BoxDecoration(
                      gradient: RadialGradient( //背景径向渐变
                        colors: [Color(0xffFF5236), Color(0xffFF6136)],
                      ),
                    ),
                    child: FlatButton(
                      child: Text('提交任务',style: TextStyle(color: Colors.white,fontSize: 17),),
                      onPressed: SubmitTaskAction,
                    ),
                  )
                ],
              )
          )
      )
    );
    throw UnimplementedError();
  }
}